package com.example.boot.controller;

import com.example.boot.bean.Person;
import com.example.boot.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author shkstart
 * @creat 2021-03-30-下午4:53
 */
@Controller
public class HelloController {
    @ResponseBody
    @GetMapping("/RequestParam")
    public String RequestParam(@RequestParam("a") Integer integer,Model model,@ModelAttribute("abc") String string){
        System.out.println(model.getAttribute("abc"));
        System.out.println(string);
        System.out.println(model.getAttribute("user").toString());
        return integer.toString();
    }
    @GetMapping("/PathVariable/{id}")
    public String PathVariable( @PathVariable("id") Integer integer){
        return integer.toString();
    }
    @PostMapping("/RequestBody")
    public Map RequestHeader(@RequestBody String content){
        Map<String,Object> map=new HashMap<>();
        map.put("content",content);
        return map;
    }
    @GetMapping("/RequestHeader")
    public Map RequestHeader( @RequestHeader("User-Agent") String userAgent,
                                 @RequestHeader Map<String,String> header){
        Map<String,Object> map = new HashMap<>();
        map.put("userAgent",userAgent);
        map.put("headers",header);
        return map;
    }

    @GetMapping("/MatrixVariable0/{path}")
    public String MatrixVariable0(@MatrixVariable("a") Integer integer ){
        return integer.toString();
    }
   //请求的路径格式为http://localhost:8080/car/sell;low=1;brand=1,2,3
    @GetMapping("/MatrixVariable1/{path}")
    public Map MatrixVariable1(@MatrixVariable("low") Integer low,
                        @MatrixVariable("brand") List<String> brand,
                        @PathVariable("path") String path){
        Map<String,Object> map = new HashMap<>();
        map.put("low",low);
        map.put("brand",brand);
        map.put("path",path);
        return map;
    }
    //请求的路径格式为http://localhost:8080/MatrixVariable2/boosId;age=1/empID;age=2,注意/MatrixVariable2/不能和其他请求相同
    //pathVar指定到{boosId}里取值，名字为value = "age",
    @GetMapping("/MatrixVariable2/{boosId}/{empId}")
    public Map MatrixVariable2(@MatrixVariable(value = "age",pathVar = "boosId") Integer boosAge,
                               @MatrixVariable(value = "age",pathVar = "empId")Integer empId){
        Map<String,Object> map = new HashMap<>();
        map.put("boosAge",boosAge);
        map.put("empAge",empId);
        return map;
    }
    @ModelAttribute
    public void ModelAttribute1(Model model){
        model.addAttribute("abc","a");
    }
    @ModelAttribute
    public User ModelAttribute2(Model model){
        User user=new User();
        return user;
    }
    @GetMapping("/params")
    public String testParam(Map<String,Object> map,
                            Model model,
                            HttpServletRequest request,
                            HttpServletResponse response){
        map.put("hello","world6666");
        model.addAttribute("world","hello6666");
        request.setAttribute("message","HelloWorld");
        Cookie cookie = new Cookie("c1","v1");
        //cookie的作用域
//        cookie.setDomain("localhost");
        response.addCookie(cookie);
        return "forward:/success";

    }
    @ResponseBody
    @GetMapping("/success")
    public Map success(HttpServletRequest request){
        Map<String,Object> map=new HashMap<>();
        Object hello = request.getAttribute("hello");
        Object world = request.getAttribute("world");
        Object message = request.getAttribute("message");

        map.put("hello",hello);
        map.put("world",world);
        map.put("message",message );
        return map;
    }
    /**
     * 数据绑定：页面提交的请求数据（无论是get、post都可以封装到对象中）
     * @param person
     * @return
     */
    @ResponseBody
    @PostMapping("/saveuser")
    public Person saveuser(Person person){
        return person;
    }

    @ResponseBody
    @GetMapping("/test/person")
    public Person person(){
        Person person = new Person();
        person.setAge(18);
        person.setBirth(new Date());
        person.setUserName("zhangsan");
        return person;
    }
    /**
     * Thymeleaf测试
     * @param
     * @return
     */
    @GetMapping("/thymeleaf")
    public String thymeleafTest(Model model){

        model.addAttribute("msg","你好 guigu");
        model.addAttribute("link","http://www.baidu.com");

        return "success";
    }
}
